﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmPhieusudungdichvu : PRE.catalog.FrmBase
    {
        DAL.PhieusudungdichvuDAL _dal = new DAL.PhieusudungdichvuDAL();
        BLL.PhieusudungdichvuBLL _bll = new BLL.PhieusudungdichvuBLL();

        public FrmPhieusudungdichvu()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus_PhieuSDDV(false);

            //string _ma = _bll.CreateID();
            lkeSTTPHIEU.Text = _sttphieu1;

            dteNGAYKHAM.Text=_ngaykham1;

            var o = new DAL.Entities.phieusudungdichvu()
            {
                sttphieu = _sttphieu1.Trim(),
                ngaykham = Convert.ToDateTime(_ngaykham1.Trim())
            };

            DataTable _dtb2 = _dal.Select_madichvuobj(o);

            lkeMADICHVU.Properties.DataSource = _dtb2;
            lkeMADICHVU.ItemIndex = 0;
          



            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        ///protected override void PerformCance()
        //{
        //   // PerformRefresh();
        //    base.PerformCancel();
        //}
        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var o = new DAL.Entities.phieusudungdichvu()
            {
                sttphieu=lkeSTTPHIEU.Text.Trim(),
                ngaykham=Convert.ToDateTime(dteNGAYKHAM.Text),
                madichvu=(lkeMADICHVU.EditValue+"").Trim()
            };
            //string id = lkeSTTPHIEU.Text.Trim();
            //string id = dteNGAYKHAM.Text;

            DialogResult result;
            var ok = _dal.deleteobj(o);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            string id = (grvMain.GetFocusedRowCellValue("MADICHVU") + "").Trim();
            DateTime ngay = Convert.ToDateTime(grvMain.GetFocusedRowCellValue("NGAYKHAM"));
            string stt= (grvMain.GetFocusedRowCellValue("STTPHIEU") + "").Trim();
            DataTable dtb_report = _dal.Select_Report(id,ngay,stt);

            var frm = new Report.Report_Base();
            var xr = new Report.XRPhieusudungdichvu();

            xr.DataSource = dtb_report;
            frm.SetReport(xr);

            frm.Show();

            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            DataTable _dtb1 = _dal.Select_madichvu();
            lkeMADICHVU.Properties.DataSource = _dtb1;
            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
           // var name = Proper(txtHoten.Text);
            //var address = Proper(txtDiachi.Text);

            if (IsAdd)
            {
                var o = new DAL.Entities.phieusudungdichvu()
                {
                  sttphieu= lkeSTTPHIEU.Text.Trim(),
                  madichvu = (lkeMADICHVU.EditValue+"").Trim(),
                  ngaykham = Convert.ToDateTime(dteNGAYKHAM.Text.Trim()),
                  ketluan="" 
                 // sttphieu= cboSTTPHIEU.EditValue +"" , 
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuSDDV(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuSDDV(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                string _sttphieu = lkeSTTPHIEU.Text;
                var o = new DAL.Entities.phieusudungdichvu()
                {

                    sttphieu = lkeSTTPHIEU.Text.Trim(),
                    madichvu = (lkeMADICHVU.EditValue + "").Trim(),
                    ngaykham = Convert.ToDateTime(dteNGAYKHAM.Text.Trim()),
                    ketluan = ""
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuSDDV();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuSDDV(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            //lkeSTTPHIEU.Text=_sttphieu;
         
            //dteNGAYKHAM.Text = _ngaykham;
           
            lkeMADICHVU.ItemIndex=-1;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
        //    lkeSTTPHIEU.DataBindings.Clear();
            
        //    dteNGAYKHAM.DataBindings.Clear();
            
           lkeMADICHVU.DataBindings.Clear();


            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
        //    lkeSTTPHIEU.DataBindings.Add("EditValue", _dtb, ".STTPHIEU");
            
        //    dteNGAYKHAM.DataBindings.Add("EditValue", _dtb, ".NGAYKHAM");
           
            lkeMADICHVU.DataBindings.Add("EditValue", _dtb, ".MADICHVU");


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            lkeSTTPHIEU.Properties.ReadOnly = true;
            dteNGAYKHAM.Properties.ReadOnly = true;
           

            lkeMADICHVU.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            var o = new DAL.Entities.phieusudungdichvu()
            {
                sttphieu = _sttphieu1.Trim(),
                ngaykham = Convert.ToDateTime(_ngaykham1.Trim())
            };
           _dtb = _dal.Select(o);

            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion


        private void FrmPhieusudungdichvu_Load(object sender, EventArgs e)
        {
            //DataTable _dtb1 = _dal.Select_sttphieu();
            //lkeSTTPHIEU.Properties.DataSource = _dtb1;

            lkeSTTPHIEU.Text = _sttphieu1;
            dteNGAYKHAM.Text = _ngaykham1;

            var o = new DAL.Entities.phieusudungdichvu()
                    {
                        sttphieu = _sttphieu1.Trim(),
                        ngaykham = Convert.ToDateTime(_ngaykham1.Trim())
                    };
            DataTable _dtb1 = _dal.Select_madichvuobj(o);
            lkeMADICHVU.Properties.DataSource = _dtb1;
            ChangeStatus_PhieuSDDV();
            PerformRefresh();
        }
        public string _sttphieu1;
        public string _ngaykham1;
        
    }
}
